/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2007 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
aLONG with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "commonheaders.h"

char *mir_strncpy(char *pszDest, const char *pszSrc, const int cchDest)
{
	if(!pszDest || !pszSrc || !cchDest)
		return NULL;
#if (_STRSAFE_USE_SECURE_CRT == 1)
	pszDest = strcpy_s(pszDest, cchDest, pszSrc);
#else
	pszDest = strncpy(pszDest, pszSrc, cchDest-1);
	pszDest[cchDest-1] = 0;
#endif
	return pszDest;
}

wchar_t *mir_wcsncpy(wchar_t *pszDest, const wchar_t *pszSrc, const int cchDest)
{
	if(!pszDest || !pszSrc || !cchDest)
		return NULL;
#if (_STRSAFE_USE_SECURE_CRT == 1)
	pszDest = wcscpy_s(pszDest, cchDest, pszSrc);
#else
	pszDest = wcsncpy(pszDest, pszSrc, cchDest-1);
	pszDest[cchDest-1] = 0;
#endif
	return pszDest;
}

char *mir_strncat(char *pszDest, const char *pszSrc, const int cchDest)
{
	if(!pszDest || !pszSrc || !cchDest)
		return NULL;
#if (_STRSAFE_USE_SECURE_CRT == 1)
	pszDest = strcat_s(pszDest, cchDest, pszSrc);
#else
	strncat(pszDest, pszSrc, cchDest-1);
	pszDest[cchDest-1] = 0;
#endif
	return pszDest;
}

wchar_t *mir_wcsncat(wchar_t *pszDest, const wchar_t *pszSrc, const int cchDest)
{
	if(!pszDest || !pszSrc || !cchDest)
		return NULL;
#if (_STRSAFE_USE_SECURE_CRT == 1)
	pszDest = wcscat_s(pszDest, cchDest, pszSrc);
#else
	pszDest = wcsncat(pszDest, pszSrc, cchDest-1);
	pszDest[cchDest-1] = 0;
#endif
	return pszDest;
}

wchar_t * mir_wcsdup(const wchar_t * str)
{
   if(str != NULL) {
		int len = wcslen(str);

		if(len > 0) {
			wchar_t *p = (wchar_t*)mir_alloc(sizeof(WCHAR) * (len + 1));
			return mir_wcsncpy(p, str, len + 1);
		}
   }
   return NULL;
}

int mir_vsnprintfA(char *pszDest, const int cchDest, const char *pszFormat, va_list& argList)
{
	int iRet, cchMax;

	if(!pszDest || !pszFormat || !*pszFormat)
		return -1;

	cchMax = cchDest - 1;
#if (_STRSAFE_USE_SECURE_CRT == 1)
	iRet = _vsnprintf_s(pszDest, cchDest, cchMax, pszFormat, argList);
#else
	iRet = _vsnprintf(pszDest, cchMax, pszFormat, argList);
#endif
	if(iRet < 0) pszDest[0] = 0;
	else if(iRet >= cchMax) {
		pszDest[cchMax] = 0;
		iRet = cchMax;
	}
	return iRet;
}

int mir_vsnprintfW(wchar_t *pszDest, const int cchDest, const wchar_t *pszFormat, va_list& argList)
{
	int iRet, cchMax;

	if(!pszDest || !pszFormat || !*pszFormat)
		return -1;

	cchMax = cchDest - 1;
#if (_STRSAFE_USE_SECURE_CRT == 1)
	iRet = _vsnwprintf_s(pszDest, cchDest, cchMax, pszFormat, argList);
#else
	iRet = _vsnwprintf(pszDest, cchMax, pszFormat, argList);
#endif
	if(iRet < 0) pszDest[0] = 0;
	else if(iRet >= cchMax) {
		pszDest[cchMax] = 0;
		iRet = cchMax;
	}
	return iRet;
}

int mir_snprintfA(char *pszDest, const int cchDest, const char *pszFormat, ...)
{
	int iRet;
	va_list argList;

	va_start(argList, pszFormat);
	iRet = mir_vsnprintfA(pszDest, cchDest, pszFormat, argList);
    va_end(argList);
	return iRet;
}

int mir_snprintfW(wchar_t *pszDest, const int cchDest, const wchar_t *pszFormat, ...)
{
	int iRet;
	va_list argList;

	va_start(argList, pszFormat);
	iRet = mir_vsnprintfW(pszDest, cchDest, pszFormat, argList);
    va_end(argList);
	return iRet;
}